package GUI.TabPanels;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;

import comparators.IntegerComparator;

import GUI.Controller.Controller;

@SuppressWarnings("serial")
public class ViewBillPanel extends JPanel {
	private JLabel bidLabel = new JLabel("Booking ID:");
	private JTextField bidText = new JTextField(9);

	private JButton view_bill = new JButton("View Bill");

	private JTable informationTable;

	private JPanel amount_panel = new JPanel();
	private JLabel amount_paid_label = new JLabel("Amount Paid:");
	private JTextField amount_paid_text = new JTextField(5);
	private JLabel slash_label = new JLabel("/");
	private JLabel total_amount_label = new JLabel("Amount Total");
	private JTextField total_amount_text = new JTextField(5);

	private JLabel pay_label = new JLabel("Payment:");
	private JTextField pay_text = new JTextField(15);

	private JButton payment = new JButton("Pay");

	private String[] columnNames =
		{
			"Charge Type", "Charge Description", "Charge Amount"
		};

	public ViewBillPanel(){
		this.setLayout(new BorderLayout());

		JPanel west = new JPanel();
		JPanel center = new JPanel();
		JPanel east = new JPanel();

		west.setBackground(new Color(147, 37, 37));
		west.add(new JLabel("      "));
		east.setBackground(new Color(147, 37, 37));
		east.add(new JLabel("      "));



		center.setLayout(new GridLayout(0, 2, -400, 7));

		center.add(bidLabel);
		center.add(bidText);

		center.add(new JLabel());
		center.add(view_bill);

		center.add(new JLabel());
		center.add(new JLabel());


		amount_panel = new JPanel();
		amount_panel.add(amount_paid_label);
		amount_panel.add(amount_paid_text);
		amount_paid_text.setEditable(false);
		amount_panel.add(slash_label);
		amount_panel.add(total_amount_label);
		amount_panel.add(total_amount_text);
		total_amount_text.setEditable(false);

		center.add(amount_panel);

		center.add(new JLabel());


		center.add(pay_label);
		center.add(pay_text);

		center.add(new JLabel());
		center.add(payment);
		payment.addActionListener(new PayBillButton());

		view_bill.addActionListener(new ViewBillButton());

		this.add(west, BorderLayout.WEST);
		this.add(center, BorderLayout.CENTER);
		this.add(east, BorderLayout.EAST);

		SetUpTable();
	}

	public void SetUpTable(){
		//Object[][] tableData = Controller.GetBills(bidText.getText());

		CreateTable(null);
		informationTable.setVisible(false);
		informationTable.setVisible(true);
	}

	void CreateTable(Object[][] tableData){
		informationTable = new JTable(tableData, columnNames){
			@Override 
			public boolean isCellEditable(int row, int column) { 
				return false; 
			} 
		};
		informationTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		DefaultTableModel model = new DefaultTableModel(tableData, columnNames);

		TableRowSorter<DefaultTableModel> sorter = new TableRowSorter<DefaultTableModel>(model);

		sorter.setComparator(2, new IntegerComparator());

		informationTable.setRowSorter(sorter);

		informationTable.setPreferredScrollableViewportSize(new Dimension(700, 320));
		informationTable.setFillsViewportHeight(true);

		JScrollPane scrollPane = new JScrollPane(informationTable);
		this.add(scrollPane, BorderLayout.SOUTH);
	}

	private class ViewBillButton implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			if(Controller.getTotalCharge(bidText.getText()) != null){
				DefaultTableModel model = new DefaultTableModel(Controller.GetBills(bidText.getText()), columnNames);

				informationTable.setModel(model);

				TableRowSorter<DefaultTableModel> sorter = new TableRowSorter<DefaultTableModel>(model);

				informationTable.setRowSorter(sorter);

				amount_paid_text.setText(Controller.getAmountPaid(bidText.getText()));
				total_amount_text.setText(Controller.getTotalCharge(bidText.getText()));
			}else{
				JOptionPane.showMessageDialog(null, "Invalid Booking ID");
			}
		}
	}

	private class PayBillButton implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			if(Controller.getTotalCharge(bidText.getText()) != null){
				try{
				Double.parseDouble(pay_text.getText());
				Controller.PayBill(bidText.getText(), pay_text.getText());
				amount_paid_text.setText(Controller.getAmountPaid(bidText.getText()));
				total_amount_text.setText(Controller.getTotalCharge(bidText.getText()));
				new ViewBillButton().actionPerformed(null);
				}catch(NumberFormatException e){
					JOptionPane.showMessageDialog(null, "Invalid Payment Amount");
				}
			}else{
				JOptionPane.showMessageDialog(null, "Invalid Booking ID");
			}
		}
	}

	public void Update(){
		System.out.println("updating view guests");
		this.setVisible(false);
		this.setVisible(true);
	}

	public void UpdateTable(){
		new ViewBillButton().actionPerformed(null);
	}
}
